// 用户筛选
import {
  customer
} from '../../../js/modules/customer'

const componentCallback = require('../../js/componentCallback')
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 隐藏哪些筛选项
    hiddenItemList: {
      type: Array,
      value: []
    },
  },

  behaviors: [componentCallback, componentBehavior],

  /**
   * 组件的初始数据
   */
  data: {
    keyword: '', // 搜索关键字
    pickerList: [], // 选择器列表
    selectedTagList: [], // 已选择标签列表
    selectedTagName: '', // 已选择标签名
    hideKeyword: false, // 隐藏顶部关键词输入框
    hideTag: false, // 隐藏tag选择器
  },

  lifetimes: {
    attached() {
      this.getFilterData()
    }
  },

  observers: {
    showComponent(showComponent) {
      if (showComponent == true) {
        let component = this.selectComponent('#offcanvas')
        component.show()
      }
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showComponent(options) {
      // console.log('其他组件参数', options)
    },
    input(e) {
      this.setData({
        keyword: e.detail.value
      })
    },
    confirm() {
      let {
        keyword,
        pickerList,
        selectedTagList
      } = this.data
      let pickerData = pickerList.map((item) => {
        return {
          key: item.key,
          item: item.list[item.selectedIndex],
          Id: item.list[item.selectedIndex].Id
        }
      })
      // let tags = selectedTagList.map((item) => {
      //   return item.Name
      // })
      let data = {
        keyword,
        pickerData,
        tags: selectedTagList
      }
      this.$$success(data)
      let component = this.selectComponent('#offcanvas')
      component.setData({
        showComponent: false
      })
    },
    reset() {
      let {
        pickerList
      } = this.data
      pickerList = pickerList.map((item) => {
        item.selectedIndex = 0
        return item
      })
      let component = this.selectComponent('#customerTag')
      component.reset()
      this.setData({
        keyword: '',
        pickerList,
        selectedTagList: [],
        selectedTagName: '',
      })
    },
    cancel() {
      this.$$fail()
    },
    picker(e) {
      let index = e.currentTarget.dataset.index
      let value = e.detail.value
      let {
        pickerList
      } = this.data
      pickerList[index].selectedIndex = value
      this.setData({
        pickerList
      })
    },
    getFilterData() {
      let {
        hiddenItemList
      } = this.data
      console.log(this.data)
      console.log(hiddenItemList)
      let pickerList = []
      let dateList = [{
          Id: -1,
          Name: '请选择'
        },
        {
          Id: 3,
          Name: '超过3天'
        },
        {
          Id: 7,
          Name: '超过7天'
        },
        {
          Id: 15,
          Name: '超过15天'
        },
        {
          Id: 30,
          Name: '超过30天'
        },
        {
          Id: 90,
          Name: '超过90天'
        }
      ]
      let promiseList = [
        customer.getCustomerLevel(),
        customer.getCustomerType(),
        customer.getCustomerWarning(),
      ]
      Promise.all(promiseList).then((res) => {
        console.log(res)
        pickerList.push({
          key: 'customerLevel',
          name: '客户等级',
          list: [{
            Id: -1,
            Name: '所有等级'
          }, ...res[0].Content],
          selectedIndex: 0
        })
        res[1].Content = res[1].Content.map((item) => {
          item.Name = item.TypeName
          return item
        })
        pickerList.push({
          key: 'customerType',
          name: '客户类型',
          list: [{
            Id: -1,
            Name: '所有类型'
          }, ...res[1].Content],
          selectedIndex: 0
        })
        pickerList.push({
          key: 'noOrderDay',
          name: '未下单天数',
          list: dateList,
          selectedIndex: 0
        })
        pickerList.push({
          key: 'noFollowDay',
          name: '未拜访天数',
          list: dateList,
          selectedIndex: 0
        })
        pickerList.push({
          key: 'arrears',
          name: '是否欠款',
          list: [{
              Id: -1,
              Name: '请选择'
            },
            {
              Id: 1,
              Name: '是'
            },
            {
              id: 0,
              Name: '否'
            }
          ],
          selectedIndex: 0
        })
        pickerList.push({
          key: 'customerWarning',
          name: '客情预警分类',
          list: [{
            Id: -1,
            Name: '所有分类'
          }, ...res[2].Content],
          selectedIndex: 0
        })
        pickerList = pickerList.map((item) => {
          if (hiddenItemList.includes(item.key)) {
            item.hidden = true
          }
          return item
        })
        let hideKeyword = hiddenItemList.includes('keyword')
        let hideTag = hiddenItemList.includes('tag')
        this.setData({
          pickerList,
          hideKeyword,
          hideTag
        })
      })
    },
    showTagSelector() {
      let that = this
      let component = this.selectComponent('#customerTag')
      component.$$showComponent({
        success(e) {
          that.setData({
            selectedTagList: e.selectedTagList || [],
            selectedTagName: e.selectedTagName || ''
          })
        }
      })
    }
  }
})